﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminShipMethod : BaseAdminModule
    {
        [AccessedThroughProperty("cmdNew")]
        private LinkButton _cmdNew;
        [AccessedThroughProperty("cmdUpdate")]
        private LinkButton _cmdUpdate;
        [AccessedThroughProperty("dgList")]
        private DataGrid _dgList;

        public AdminShipMethod()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        private void cmdNew_Click(object sender, EventArgs e)
        {
            try
            {
                ShipController controller = new ShipController();
                NB_Store_ShippingMethodInfo objInfo = new NB_Store_ShippingMethodInfo {
                    PortalID = this.PortalId,
                    ShipMethodID = -1,
                    MethodDesc = "",
                    MethodName = "",
                    SortOrder = 1,
                    TemplateName = "",
                    URLtracker = ""
                };
                controller.UpdateObjShippingMethod(objInfo);
                this.populateList();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                this.UpdateList();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void dgList_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            DataGridItem item = e.Item;
            int shipMethodID = int.Parse(e.CommandArgument.ToString());
            ShipController controller = new ShipController();
            NB_Store_ShippingMethodInfo shippingMethod = controller.GetShippingMethod(shipMethodID);
            if ((shippingMethod != null) && shippingMethod.Disabled)
            {
                controller.DeleteShippingMethod(shippingMethod.ShipMethodID);
            }
            this.populateList();
        }

        private void dgList_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Copy")
            {
                DataGridItem item = e.Item;
                int shipMethodID = int.Parse(e.CommandArgument.ToString());
                new ShipController().CopyShippingMethod(shipMethodID);
                this.populateList();
            }
        }

        private void dgList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGridItem item = e.Item;
            if (((item.ItemType == ListItemType.Item) | (item.ItemType == ListItemType.AlternatingItem)) | (item.ItemType == ListItemType.SelectedItem))
            {
                Control control = item.Controls[0].Controls[0];
                if (control is ImageButton)
                {
                    ImageButton button = (ImageButton) control;
                    if ((item.ItemIndex == 0) | !Conversions.ToBoolean(DataBinder.Eval(RuntimeHelpers.GetObjectValue(item.DataItem), "Disabled")))
                    {
                        button.Visible = false;
                        if (item.ItemIndex == 0)
                        {
                            ((CheckBox) item.FindControl("chkDisabled")).Visible = false;
                        }
                    }
                    else
                    {
                        button.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdDeleteMsg", this.LocalResourceFile) + "');");
                        button.ToolTip = DotNetNuke.Services.Localization.Localization.GetString("cmdDeleteTip", this.LocalResourceFile);
                    }
                }
                DropDownList ddlTemplates = (DropDownList) item.FindControl("ddlTemplateName");
                if (ddlTemplates != null)
                {
                    SharedFunctions.populateTemplateList(this.PortalId, ddlTemplates, ".shiptemplate", "NONE", DotNetNuke.Services.Localization.Localization.GetString("None", this.LocalResourceFile), Conversions.ToString(DataBinder.Eval(RuntimeHelpers.GetObjectValue(item.DataItem), "TemplateName")));
                }
                Control control2 = item.Controls[8].Controls[0];
                if (control2 is ImageButton)
                {
                    ImageButton button2 = (ImageButton) control2;
                    button2.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdCopyMsg", this.LocalResourceFile) + "');");
                    button2.ToolTip = DotNetNuke.Services.Localization.Localization.GetString("cmdCopyTip", this.LocalResourceFile);
                }
                DropDownList list2 = (DropDownList) item.FindControl("ddlTrackerTemplate");
                if (list2 != null)
                {
                    SharedFunctions.populateTemplateList(this.PortalId, list2, ".tracktemplate", "NONE", DotNetNuke.Services.Localization.Localization.GetString("None", this.LocalResourceFile), Conversions.ToString(DataBinder.Eval(RuntimeHelpers.GetObjectValue(item.DataItem), "URLTracker")));
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.Page.IsPostBack)
                {
                    this.populateList();
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void populateList()
        {
            ShipController controller = new ShipController();
            DataGrid dgList = this.dgList;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgList, this.LocalResourceFile);
            this.dgList = dgList;
            ArrayList shippingMethodList = controller.GetShippingMethodList(this.PortalId);
            this.dgList.DataSource = shippingMethodList;
            this.dgList.DataBind();
        }

        private void UpdateList()
        {
            if (this.Page.IsValid)
            {
                IEnumerator enumerator=null;
                ShipController controller = new ShipController();
                try
                {
                    enumerator = this.dgList.Items.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        DataGridItem current = (DataGridItem) enumerator.Current;
                        NB_Store_ShippingMethodInfo shippingMethod = controller.GetShippingMethod(Conversions.ToInteger(current.Cells[1].Text));
                        if (shippingMethod != null)
                        {
                            shippingMethod.MethodName = ((TextBox) current.FindControl("txtMethodName")).Text;
                            shippingMethod.MethodDesc = ((TextBox) current.FindControl("txtMethodDesc")).Text;
                            shippingMethod.TemplateName = ((DropDownList) current.FindControl("ddlTemplateName")).SelectedValue;
                            shippingMethod.URLtracker = ((DropDownList) current.FindControl("ddlTrackerTemplate")).SelectedValue;
                            shippingMethod.Disabled = ((CheckBox) current.FindControl("chkDisabled")).Checked;
                            shippingMethod.SortOrder = Conversions.ToInteger(((TextBox) current.FindControl("txtSortOrder")).Text);
                            shippingMethod.PortalID = this.PortalId;
                            shippingMethod.Dealer = ((CheckBox) current.FindControl("chkDealer")).Checked;
                            shippingMethod.NonDealer = ((CheckBox) current.FindControl("chkNonDealer")).Checked;
                            controller.UpdateObjShippingMethod(shippingMethod);
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                this.populateList();
            }
        }

        protected virtual LinkButton cmdNew
        {
            get
            {
                return this._cmdNew;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdNew_Click);
                if (this._cmdNew != null)
                {
                    this._cmdNew.Click -= handler;
                }
                this._cmdNew = value;
                if (this._cmdNew != null)
                {
                    this._cmdNew.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdUpdate
        {
            get
            {
                return this._cmdUpdate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdUpdate_Click);
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click -= handler;
                }
                this._cmdUpdate = value;
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click += handler;
                }
            }
        }

        protected virtual DataGrid dgList
        {
            get
            {
                return this._dgList;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridCommandEventHandler handler = new DataGridCommandEventHandler(this.dgList_ItemCommand);
                DataGridCommandEventHandler handler2 = new DataGridCommandEventHandler(this.dgList_DeleteCommand);
                DataGridItemEventHandler handler3 = new DataGridItemEventHandler(this.dgList_ItemDataBound);
                if (this._dgList != null)
                {
                    this._dgList.ItemCommand -= handler;
                    this._dgList.DeleteCommand -= handler2;
                    this._dgList.ItemDataBound -= handler3;
                }
                this._dgList = value;
                if (this._dgList != null)
                {
                    this._dgList.ItemCommand += handler;
                    this._dgList.DeleteCommand += handler2;
                    this._dgList.ItemDataBound += handler3;
                }
            }
        }
    }
}

